package com.ler.yrmall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ler.yrmall.basic.Result;
import com.ler.yrmall.product.entity.AttrAttrGroupRelationEntity;
import com.ler.yrmall.product.entity.AttrEntity;
import com.ler.yrmall.product.form.AttrForm;
import com.ler.yrmall.product.service.AttrAttrgroupRelationService;
import com.ler.yrmall.product.service.AttrService;
import com.ler.yrmall.product.vo.AttrVO;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;


/**
 * @author Leron
 */
@RestController
@RequestMapping("/attr")
public class AttrController {

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result<?> list(AttrForm request) {
        return Result.ok(attrService.list(request));
    }

    /**
     * 根据类型和分类id查询
     * @param request
     */
    @RequestMapping("/find")
    public Result<?> find(AttrForm request) {
        return Result.ok(attrService.find(request));
    }


    /**
     * 信息
     */
    @RequestMapping("/getById/{attrId}")
    public Result<?> getById(@PathVariable Long attrId) {
        AttrVO vo = attrService.getVoById(attrId);
        return Result.ok(vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result<?> save(@RequestBody AttrForm request) {
        attrService.saveAttr(request);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result<?> update(@RequestBody AttrEntity attr) {
        //TODO 回显信息 如果是更新基本属性需要更新关联属性
        attrService.updateById(attr);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result<?> delete(@RequestBody Long[] attrIds) {
        //TODO 如果是基本属性需要删除关联属性
        attrService.removeByIds(Arrays.asList(attrIds));
        attrAttrgroupRelationService.remove(Wrappers.<AttrAttrGroupRelationEntity>lambdaQuery()
                .in(AttrAttrGroupRelationEntity::getAttrId,Arrays.asList(attrIds)));
        return Result.ok();
    }

}
